

# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/sample_regressions.log", text replace;


*-------------------------****** OUTPUT SETTINGS *****---------------------;
*settings some settings that are common for the set of plots on this dofile; 
local pdf_plot_settings = 	`"scale(*1.5) ymtick(##4) "';

*settings for latex tables ;

		global prehead_start "prehead(\begin{table}[H]\centering 
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  
		\caption{@title}
		\setlength{\tabcolsep}{1pt}";
		
		global prehead_start_longcol "prehead(\begin{table}[!htb]\centering 
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  
		\caption{@title}
		\setlength{\tabcolsep}{6pt}";
		
		local font_size "";
		global prehead_end         "\begin{tabular}{l*{@span}{c}}\hline\hline)";
		global prehead_end_bigskip "\begin{tabular}{l*{@span}{c}}\hline\hline \bigskip\\[6ex])";
		global postfoot "postfoot(\hline\hline
		\multicolumn{@span}{l}{\footnotesize Standard Errors in Parentheses Clustered at the State-Year Level. }\\
			\multicolumn{@span}{l}{\footnotesize  @starlegend. }
		\end{tabular}\end{table})";
		
		
		
		
		
			local tex_settigns  = `"
		${prehead_start}
		`font_size'
		${prehead_end}
		${postfoot}
		"';	



*----------------------------------------------------------------------------------;



use "../data/hpms_hwy_stats_88_08.dta", clear  ;


*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;
do "_programs_sample";
do "_setup_expenditure.do";
do "_cleaning_and_new_variables_sample.do";

** generate a variable of average number of years between maintenance in a segment;

bysort id (year): egen N_maint=total(I2_t);
bysort id (year): egen xx=count(year);
gen av_year_maint=xx/(N_maint+1);


label var av_year_maint "mean t for I in seg. i";
tab av_year_maint;
drop  xx;

*------------------------------------------------; 
*regressions; 
*-----------------------------------------------;

*inches per mile;

tab year;

local listcontinous "c.time cont Baseline" ;
local listcontinous2 "c.time_+_c.time2 cont_2" ;
local listdiscrete  "ib2.periods dum Periods" ;

gen no_trucks_aadt=aadt_10000 - trucks_av_10000;
label var no_trucks_aadt "No trucks AADT/10000";
label var trucks_av_10000 "Trucks AADT/10000";

gen I2_t_D_rig=I2_t*D_rig ;


local keep_rule_all "if 1==1 ";
local keep_rule_after1992 "if year>1992" ;




	local time 	        = word("`listcontinous'",1) ;
	local time `:subinstr local time `"_+_"' `" "', ';
	local name 	        = word("`listcontinous'",2);
	local title 	    = word("`listcontinous'",3); 
	
	gen I2_t_exp_I2Xtime=I2_t_exp_I2*time;
	gen I2_t_app_I2Xtime=I2_t_app_I*time;
	label var I2_t_exp_I2Xtime "$ \mathds{1}_{ist}(q)\imath^q_{st}\times t$";

	label var  time  "$ t $";
	label var I2_t_D_rig "$\mathds{1}_{ist}(q) \times rigid $";
	
	
		local aadt 	        = word("`list`a''",1) ;
		local aadt `:subinstr local aadt `"_+_"' `" "', all ';
		local aadt_name 	 = word("`list`a''",2);
	macro dir;
	
   local cluster_var state_year;	
   	local tit_state "State";
	local tit_state_year "State-year";
	global SE_note "Standard Errors in Parentheses Clustered at the `tit_`cluster_var'' Level";

if 1==1{;	
	eststo ,prefix(OLS_): reg Diri  io0.I2_t   ,  cluster(`cluster_var');
	sum_stats;

	eststo ,prefix(OLS_): reghdfe  Diri  io0.I2_t    ,  abs( state_year)  cluster(`cluster_var');
	sum_stats;
	
	eststo, prefix(OLS_) :reghdfe  Diri  io0.I2_t     , abs(id state_year)  cluster(`cluster_var') ;
	sum_stats;

		
	eststo ,prefix(OLS_): reg Diri   io0.I2_t##(`time')   ,  cluster(`cluster_var');
	sum_stats;

	eststo ,prefix(OLS_): reghdfe  Diri   io0.I2_t##(`time')   , abs(state)  cluster(`cluster_var');//texas omitted
	sum_stats;

	
	eststo, prefix(OLS_): reghdfe Diri   io0.I2_t##(`time')  , abs(id )   cluster(`cluster_var') ;
	sum_stats;



	estfe . OLS_*, labels(state "State FE" state_year "State-Year FE" id "Segment id FE");

	esttab OLS_* using "${output}/tables/TableB1_sample_regressions1_I2_t_SE`cluster_var'.tex", replace
		b(%8.2fc) se(%8.2fc)  
		star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
		order( )
		title("Resurfacing and IRI \label{tab:IRI_resurfacing}")
		indicate(`r(indicate_fe)' )
		nomtitles  noomitted   nobaselevels 
	    scalars( "mean_Diri Mean \( \Delta Q_{st} \)"  "mean_aadt Mean AADT" ) 
		substitute(\_ _  "I2_t=1" "$ \mathds{1}_{ist}(q) $"
		"State FE" "\cline{2-7}State FE")
		se  par   r2 label nogaps  drop(_cons c1 ) compress
	obslast stats(N, fmt(%15.0fc))
		${prehead_start_longcol}
		`font_size'
		${prehead_end}
		${postfoot}		;
};
	eststo clear ;
	
	*IV Definition:;
	*----------------------------;


	*expenditure per resurfaced mile;
	local IV I2_t_L4_apport_I2;
	label var I2_t_L4_apport_I2 "\( \mathds{1}_{ist}(q)app_{s(t-4)} \right) \)";
	gen `IV'Xtime =`IV'*time;
	label var `IV'Xtime "\( \mathds{1}_{ist}(q)app_{s(t-4)} \right)  t \)";
	gen `IV'_sample=1 if `IV'!=.;
	
	
	*expenditure per resurfaced mile;
	sort id year ;
	
	eststo ,prefix(table_): reg Diri  c.I2_t_exp_I2  ,  cluster(`cluster_var');
	sum_stats;

	eststo ,prefix(table_): reghdfe Diri  c.I2_t_exp_I2  , abs( state_year )    cluster(`cluster_var');
	sum_stats;
	
	
	eststo, prefix(table_) : reghdfe Diri  c.I2_t_exp_I2    , abs(id state_year )   cluster(`cluster_var') ;
	sum_stats;

		
	eststo ,prefix(table_): reg Diri c.I2_t_exp_I2 time  I2_t_exp_I2Xtime   ,  cluster(`cluster_var');
	sum_stats;

	eststo ,prefix(table_): reghdfe    Diri  c.I2_t_exp_I2 time  I2_t_exp_I2Xtime   ,abs(state )   cluster(`cluster_var');//texas omitted
	sum_stats;

	eststo, prefix(table_) : reghdfe  Diri  c.I2_t_exp_I2 time  I2_t_exp_I2Xtime   , abs(state_year ) cluster(`cluster_var') ;
	sum_stats;
	
	eststo, prefix(table_) : reghdfe  Diri  c.I2_t_exp_I2 time  I2_t_exp_I2Xtime   , abs(id ) cluster(`cluster_var') ;
	sum_stats;
	
	eststo, prefix(table_) : reghdfe  Diri  c.I2_t_exp_I2 time  I2_t_exp_I2Xtime   , abs(id state_year) cluster(`cluster_var') ;
	sum_stats;
	

	estfe . table_1 table_2 table_3 table_4 table_5 table_7   , labels(state "State FE" state_year "State-Year FE" id "Segment id FE");
	return list;
	
	
	
	esttab table_1 table_2 table_3 table_4 table_5 table_7 using "${output}/tables/Table2_sample_regressions.tex", replace
		b(%8.2fc) se(%8.2fc)  
		order( I2_t_exp_I2 time )
				star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
		title("Resurfacing expenditure and IRI\label{tab:IRI_expenditure}")
		
		indicate( `r(indicate_fe)')
		nomtitles  noomitted   nobaselevels  
		scalars( "mean_Diri Mean \( \Delta Q_{st} \)"  "mean_aadt Mean AADT"   ) 
		substitute(\_ _ "I2_t=1" "$\mathds{1}_{ist}(q)$"
					"State FE" "\cline{2-7}State FE")
		se  par  r2 label nogaps drop(_cons c1) compress
	obslast stats(N , fmt(%15.0fc)  labels(`"N"' ))
		`tex_settigns'
		;


	

eststo clear;	

*-----------------------------------;
* - I_t on IRI ; 
*-----------------------------------;

ren I2_t_x* I2_t_x_T* ;

eststo: reg Diri  I2_t_x_T*  ,  cluster(state) ;
plot_dummies;
copy "temp_t.pdf" "${output}/figures/FigureB1_year_X_I2_1.pdf", replace;
erase "temp_t.pdf";
erase "temp_betas.dta";
ren  I2_t_x_T* I2_t_x*;

ren I2_t_*_exp_I2 I2_t_x_T* ;

eststo: reg Diri  I2_t_x_T*  ,  cluster(state) ;
plot_dummies;
copy "temp_betas.dta" "../intermediate_data/resurf_betas_1.dta", replace ;

erase "temp_t.pdf";
erase "temp_betas.dta";

ren  I2_t_x_T* I2_t_*_exp_I2;
log close;
exit;
